
//var remote_server = "http://localhost:1145"; // Change this to your remote server address
var remote_server = "https://db.linwinsoft.top"; // Change this to your remote server address

class DBAPI {
    username = null;
    password = null;

    set_login(username, password) {
        this.username = username;
        this.password = password;
    }

    async send_command(command) {
        const url = remote_server + "/" + this.username + "/" + this.password + "/" + command;
        const response = await fetch(url);
        const data = await response.json();
        return data;
    }
    invoke_api(username, password,command, callback) {
        const api = new DBAPI();
        api.set_login(username, password);
        api.send_command(command).then(data => {
            callback(data);
        });
    }
    show_databases(callback) {
        this.invoke_api(this.username, this.password, "show databases", callback);
    }

    list_databases(db_name,callback) {
        this.invoke_api(this.username, this.password, "list "+db_name, callback);
    }


    create_database(db_name,callback) {
        this.invoke_api(this.username, this.password, "create database "+db_name, callback);
    }

    delete_database(db_name,callback) {
        this.invoke_api(this.username, this.password, "drop "+db_name, callback);
    }

    show_data_keys_from_database(data_name,db_name,callback) {
        this.invoke_api(this.username, this.password, "show "+data_name+" from "+db_name, callback);
    }

    get_key_value_from_database(data_name,key,db_name,callback) {
        this.invoke_api(this.username, this.password, "get "+data_name+"."+key+" from "+db_name, callback);
    }

    update_key_value_in_database(data_name,key,value,db_name,callback) {
        this.invoke_api(this.username, this.password, "update key "+data_name+"."+key+"="+value+" from "+db_name, callback);
    }

    delete_key_value_from_database(data_name,key,db_name,callback) {
        this.invoke_api(this.username, this.password, "delete key "+data_name+"."+key+" from "+db_name, callback);
    }

    delete_data_from_database(data_name,db_name,callback) {
        this.invoke_api(this.username, this.password, "delete data "+data_name+" from "+db_name, callback);
    }

    search_databasename_from_database(search_string,callback) {
        this.invoke_api(this.username, this.password, "index "+search_string, callback);
    }

    insert_data_in_database(data_name,db_name,keys,callback) {
        // keys = "key1=1,key2=2"
        this.invoke_api(this.username, this.password, "insert into "+db_name+" "+data_name+" "+keys, callback);
    }

    create_key_to_database(data_name,key_name,db_name,callback) {
        this.invoke_api(this.username, this.password, "create key "+key_name+" to "+db_name+"."+data_name, callback);
    }

    select_data_from_database(data_name,db_name,callback) {
        this.invoke_api(this.username, this.password, "select "+data_name+" from "+db_name, callback);
    }

    show_stratures_from_database(db_name,callback) {
        // 需要组成一个完整的 Json 结构
        // 利用的是内置的几个方法组合
        // 例如展示一个数据库的结构，先获取他下面所有的数据表，然后再获取每个数据表的结构
        // 再获取每个数据表的索引，最后组成一个完整的 Json 结构
        // 以下功能需要自己实现
        // 需要异步阻塞



        // 先获取数据库下面所有的数据表
        this.invoke_api(this.username, this.password, "list "+db_name, (data) => {
            // 然后获取每个数据表的结构
            let tables = data.result;

            let table_structures = {};
            for(let i=0;i<tables.length;i++){
                let table_name = tables[i];
                this.invoke_api(this.username, this.password, "show "+table_name+" from "+db_name, (data) => {
                    table_structures[table_name] = data.result;
                    // 最后获取每个数据表的索引
                    this.invoke_api(this.username, this.password, "index "+table_name+" from "+db_name, (data) => {
                        table_structures[table_name]["index"] = data.result;
                        // 组成一个完整的 Json 结构
                        let result = {
                            "database_name": db_name,
                            "table_structures": table_structures
                        };
                        callback(result);
                    });
                });   
            }
        });
    }
}